#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
添加择偶条件多选偏好字段的脚本
"""

from app import create_app, db
from sqlalchemy import text

def add_mate_criteria_preferences():
    """添加择偶条件多选偏好字段"""
    app = create_app()
    
    with app.app_context():
        try:
            # 添加新字段
            sql_commands = [
                "ALTER TABLE mate_criteria ADD COLUMN education_preferences TEXT COMMENT '学历偏好JSON数组'",
                "ALTER TABLE mate_criteria ADD COLUMN income_preferences TEXT COMMENT '收入偏好JSON数组'",
                "ALTER TABLE mate_criteria ADD COLUMN occupation_preferences TEXT COMMENT '职业偏好JSON数组'",
                "ALTER TABLE mate_criteria ADD COLUMN marital_preferences TEXT COMMENT '婚姻状况偏好JSON数组'",
                "ALTER TABLE mate_criteria ADD COLUMN housing_preferences TEXT COMMENT '住房偏好JSON数组'",
                "ALTER TABLE mate_criteria ADD COLUMN car_preferences TEXT COMMENT '购车偏好JSON数组'",
                "ALTER TABLE mate_criteria ADD COLUMN smoking_preferences TEXT COMMENT '吸烟偏好JSON数组'",
                "ALTER TABLE mate_criteria ADD COLUMN drinking_preferences TEXT COMMENT '饮酒偏好JSON数组'",
                "ALTER TABLE mate_criteria ADD COLUMN personality_preferences TEXT COMMENT '性格偏好JSON数组'",
                "ALTER TABLE mate_criteria ADD COLUMN city_preferences TEXT COMMENT '城市偏好JSON数组'",
                "ALTER TABLE mate_criteria ADD COLUMN has_children_preference INTEGER DEFAULT -1 COMMENT '对方是否有孩子：-1不限0无1有'",
                "ALTER TABLE mate_criteria ADD COLUMN wants_children_preference INTEGER DEFAULT -1 COMMENT '对方是否要孩子：-1不限0不要1要'"
            ]
            
            for sql in sql_commands:
                try:
                    db.session.execute(text(sql))
                    print(f"✅ 执行成功: {sql}")
                except Exception as e:
                    if "Duplicate column name" in str(e):
                        print(f"⚠️  字段已存在: {sql}")
                    else:
                        print(f"❌ 执行失败: {sql}")
                        print(f"   错误: {str(e)}")
            
            db.session.commit()
            print("✅ 择偶条件偏好字段添加完成")
            
        except Exception as e:
            db.session.rollback()
            print(f"❌ 添加字段失败: {str(e)}")

if __name__ == '__main__':
    add_mate_criteria_preferences() 